var gVisibleContent;

function nccChanged()
{
	// check if the sidebar is opened
	var sidebar = document.getElementById("sidebar") ||
    parent.document.getElementById("sidebar");
	if (sidebar.docShell == null) return;

	// else update the navtree
	var tree = sidebar.contentWindow.document.getElementById("navtree");
	sidebar.contentWindow.gVisibleContent = createVisibleContent(gNavData);
	tree.view = new treeView(gNavData,sidebar.contentWindow.gVisibleContent);

}

function createVisibleContent(navData)
{
	var visible = new Array();
	if(navData==undefined) return visible;
	var j = 0;
	for (var i=0; i < navData.length; i++) {
		if (navData[i].level==1){
			visible[j++]=[i,false];
		}
	};
	return visible;
}

function initNavTree()
{
	var data = parent.gNavData;
	gVisibleContent = createVisibleContent(data);
	document.getElementById("navtree").view = new treeView(data,gVisibleContent);
}
function treeView(data,visible) 
{
	this.treeBox= null;
	this.selection= null;
	this.rowCount = visible.length;
	this.setTree = function(treeBox)
	{
		this.treeBox = treeBox; 
	};
	this.getCellText = function(idx, column) 
	{
		return data[visible[idx][0]].label; 
	};
	this.isContainerEmpty = function(idx)
	{
		var item = data[visible[idx][0]];
		var next = data[visible[idx][0]+1];
		if(next && next.level>item.level) return false; 
		else return true; 
	};
	this.isContainerOpen= function(idx)     
	{
		return visible[idx][1]; 
	};
	this.isContainer= function(idx)    { return true; };
	this.isSeparator= function(idx)         { return false; };
	this.isSorted= function()               { return false; };
	this.isEditable= function(idx, column)  { return false; };

	this.getParentIndex= function(idx) 
	{
		var item = data[visible[idx][0]];
		if (item.level==1) return -1;
		for (var t = idx - 1; t >= 0 ; t--) 
		{
			var prev = data[visible[t][0]];
			if (prev.level < item.level) return t;
		}
		return -1;
	};
	this.getLevel= function(idx) 
	{
		return data[visible[idx][0]].level-1;
	};
	this.hasNextSibling= function(idx, after) 
	{
		var thisLevel = this.getLevel(idx);
		for (var t = idx + 1; t < visible.length; t++) 
		{
			var nextLevel = this.getLevel(t);
			if (nextLevel == thisLevel) return true;
			else if (nextLevel < thisLevel) return false;
		}
		return false;
	};
	this.toggleOpenState= function(idx) 
	{
		var item = visible[idx];
		if (!this.isContainer(idx)) return;

		if (item[1]) 
		{
			item[1] = false;

			var thisLevel = this.getLevel(idx);
			var deletecount = 0;
			for (var t = idx + 1; t < visible.length; t++) 
			{
				if (this.getLevel(t) > thisLevel) deletecount++;
				else break;
			}
			if (deletecount) 
			{
				visible.splice(idx + 1, deletecount);
				this.treeBox.rowCountChanged(idx + 1, -deletecount);
			}
		}
		else 
		{
			item[1] = true;

			var navItem = data[visible[idx][0]];
			var toinsert = new Array();
			var j = 0;
			for (var i=visible[idx][0]+1; i < data.length; i++) 
			{
				if (data[i].level==parseInt(navItem.level)+1) 
				{
					toinsert[j++]=[i,false];
				}
				else if (data[i].level <= navItem.level) 
				{
					break;
				}
			};
			for (var i = 0; i < toinsert.length; i++) 
			{
				visible.splice(idx + i + 1, 0, toinsert[i]);
			}
			this.treeBox.rowCountChanged(idx + 1, toinsert.length);
		}
	};

	this.getImageSrc= function(idx, column) {};
	this.getProgressMode= function(idx,column) {};
	this.getCellValue= function(idx, column) {};
	this.cycleHeader= function(col, elem) {};
	this.selectionChanged= function() {};
	this.cycleCell=function(idx, column) {};
	this.performAction= function(action) {};
	this.performActionOnCell= function(action, index, column) {};
	this.getRowProperties=function(idx, column, prop) {};
	this.getCellProperties= function(idx, column, prop) {};
	this.getColumnProperties= function(column, element, prop) {};
}

function onTreeSelected()
{
	var tree = document.getElementById("navtree");
	var navItem = parent.gNavData[gVisibleContent[tree.currentIndex][0]];
	//alert(navItem.contentSrc);
	parent.book.loadSmilUrl(navItem.contentSrc);
}

function jsdump(str)
{
	Components.classes['@mozilla.org/consoleservice;1']
	.getService(Components.interfaces.nsIConsoleService)
	.logStringMessage(str);
}
